package com.pyue.eam.regi.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.aizuda.limiter.annotation.RateLimit;
import com.aizuda.limiter.strategy.IpKeyGenerateStrategy;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyue.eam.admin.api.constant.ResponseWord;
import com.pyue.eam.admin.api.entity.regi.ClientLibEntity;
import com.pyue.eam.admin.api.entity.regi.PartLibEntity;
import com.pyue.eam.common.core.util.R;
import com.pyue.eam.common.excel.annotation.ResponseExcel;
import com.pyue.eam.common.log.annotation.SysLog;
import com.pyue.eam.regi.service.PartLibService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 零件库
 *
 * @author lucc
 * @date 2023-10-12 20:38:39
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/partLib")
@Tag(description = "partLib", name = "零件库管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PartLibController {
    private final PartLibService partLibService;

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param partLibEntity 零件库
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    // @PreAuthorize("@pms.hasPermission('eam_partLib_view')" )
    public R getPartLibPage(@ParameterObject Page page, @ParameterObject PartLibEntity partLibEntity) {
        LambdaQueryWrapper<PartLibEntity> wrapper = Wrappers.lambdaQuery(partLibEntity).orderByDesc(PartLibEntity::getCreateTime);
        return R.ok(partLibService.page(page, wrapper));
    }


    /**
     * 通过id查询零件库
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    // @PreAuthorize("@pms.hasPermission('eam_partLib_view')" )
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(partLibService.getById(id));
    }

    /**
     * 新增零件库
     *
     * @param partLibEntity 零件库
     * @return R
     */
    @Operation(summary = "新增零件库", description = "新增零件库")
    @SysLog("新增零件库")
    @PostMapping
    @RateLimit(count = 1, interval = "3s", strategy = {IpKeyGenerateStrategy.TYPE}, message = ResponseWord.WAIT_TIME)
    // @PreAuthorize("@pms.hasPermission('eam_partLib_add')" )
    public R save(@RequestBody PartLibEntity partLibEntity) {
        return R.ok(partLibService.save(partLibEntity));
    }

    /**
     * 修改零件库
     *
     * @param partLibEntity 零件库
     * @return R
     */
    @Operation(summary = "修改零件库", description = "修改零件库")
    @SysLog("修改零件库")
    @PutMapping
    // @PreAuthorize("@pms.hasPermission('eam_partLib_edit')" )
    public R updateById(@RequestBody PartLibEntity partLibEntity) {
        return R.ok(partLibService.updatePartLib(partLibEntity));
    }

    /**
     * 通过id删除零件库
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除零件库", description = "通过id删除零件库")
    @SysLog("通过id删除零件库")
    @DeleteMapping
    // @PreAuthorize("@pms.hasPermission('eam_partLib_del')" )
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(partLibService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param partLibEntity 查询条件
     * @param ids           导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    // @PreAuthorize("@pms.hasPermission('eam_partLib_export')" )
    public List<PartLibEntity> export(PartLibEntity partLibEntity, Integer[] ids) {
        return partLibService.list(Wrappers.lambdaQuery(partLibEntity).in(ArrayUtil.isNotEmpty(ids), PartLibEntity::getId, ids));
    }

    /**
     * 快速匹配料号 todo 通过关键词
     */


}
